package com.wenyu.wenyuyuedu.adapter.personage;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.bean.personage.MyMessagesBean;
import com.wenyu.wenyuyuedu.utils.TimeStyle;
import com.wenyu.wenyuyuedu.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/25.
 * 我的消息中---评论的adapter
 */

public class MyZanAdapter extends BaseAdapter {
    private List<MyMessagesBean.InfoBean> infoBeen = new ArrayList<>();
    private Context context;

    private MyCollectAdapter.deleteOnclickListener deleteOnclickListener;
    private String id;
    private MyMessagesBean.InfoBean infoBean;

    public void setDeleteOnClickLis(MyCollectAdapter.deleteOnclickListener deleteOnClickLis) {
        this.deleteOnclickListener = deleteOnClickLis;

    }

    public MyZanAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return infoBeen.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        MyCommentViewHolder holder = null;
        if (view == null) {
            holder = new MyCommentViewHolder();
            view = View.inflate(context, R.layout.my_messages_zan_item, null);
            holder.civ_headview = (CircleImageView) view.findViewById(R.id.civ_headview);
            holder.tv_comment_item_name = (TextView) view.findViewById(R.id.tv_comment_item_name);
            holder.tv_comment_item_time = (TextView) view.findViewById(R.id.tv_comment_item_time);
            holder.tv_comment_item_content = (TextView) view.findViewById(R.id.tv_comment_item_content);
            holder.tv_comment_circle_name = (TextView) view.findViewById(R.id.tv_comment_circle_name);
            holder.tv_comment_item_huifu = (TextView) view.findViewById(R.id.tv_comment_item_huifu);
            holder.iv_comment_item_content = (TextView)view.findViewById(R.id.iv_comment_item_content);
            holder.iv_comment_item_conver = (ImageView) view.findViewById(R.id.iv_comment_item_conver);
            holder.tv = (RelativeLayout) view.findViewById(R.id.tv);
            holder.btn_delete = (Button) view.findViewById(R.id.btn_delete);
            view.setTag(holder);
        } else {
            holder = (MyCommentViewHolder) view.getTag();
        }
        infoBean = infoBeen.get(i);
        if (infoBean != null) {
            /*该条通知的id*/
            id = infoBean.getId();
            /*已读未读状态*/
            String is_check = infoBean.getIs_check();
            int i1 = Integer.parseInt(is_check);
            if (i1 == 0) {
                holder.tv.setBackgroundResource(R.color.weidu);
            } else if (i1 == 1) {
                holder.tv.setBackgroundResource(R.color.yidu);
            }
            /*操作的头像*/
            Glide.with(context)
                    .load(infoBean.getUser_icon())
                    .error(R.drawable.login_logo)
                    .into(holder.civ_headview);
            /*评论的昵称*/
            holder.tv_comment_item_name.setText(infoBean.getPublisher_nickname());
            /*评论的时间*/
            String pubtime = infoBean.getPubtime();
            String s = TimeStyle.publishTime(pubtime);
            holder.tv_comment_item_time.setText(s);
            /*判断服务器返回的是圈子图片还是文字*/
            String circle_cover_type = infoBean.getCircle_cover_type();
            if(circle_cover_type.equals("images")){
               /*圈子的图片*/
                holder.iv_comment_item_conver.setVisibility(View.VISIBLE);
                holder.iv_comment_item_content.setVisibility(View.GONE);
                Glide.with(context)
                        .load(infoBean.getCircle_cover())
                        .error(R.drawable.login_logo)
                        .into(holder.iv_comment_item_conver);

            }else if(circle_cover_type.equals("word")){
                String circle_cover = infoBean.getCircle_cover();
                int length1 = circle_cover.length();
               /*文字*/
                if(length1 <= 18){
                    holder.iv_comment_item_conver.setVisibility(View.GONE);
                    holder.iv_comment_item_content.setVisibility(View.VISIBLE);
                    holder.iv_comment_item_content.setText(circle_cover);
                }else {
                    String substring = circle_cover.substring(0, 18);
                    holder.iv_comment_item_conver.setVisibility(View.GONE);
                    holder.iv_comment_item_content.setVisibility(View.VISIBLE);
                    holder.iv_comment_item_content.setText(substring + "...");
                }
            }
            /*评论的内容*/
            holder.tv_comment_item_content.setText(infoBean.getComment_date());
            /*圈子的名字*/
            holder.tv_comment_circle_name.setText(infoBean.getCircle_title());

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (deleteOnclickListener != null) {
                        deleteOnclickListener.onClick(i, view);
                    }
                }
            });
            holder.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (deleteOnclickListener != null) {
                        deleteOnclickListener.onClick(i, view);
                    }
                }
            });
        }

        return view;
    }

    public List<MyMessagesBean.InfoBean> getList() {
        return infoBeen;
    }

    class MyCommentViewHolder {
        CircleImageView civ_headview;
        TextView tv_comment_item_name, tv_comment_item_time, tv_comment_item_content, tv_comment_circle_name, tv_comment_item_huifu,iv_comment_item_content;
        ImageView iv_comment_item_conver;
        RelativeLayout tv;
        Button btn_delete;
    }

    public void addAll(List<MyMessagesBean.InfoBean> infoBeen) {
        if (infoBeen != null) {
            this.infoBeen = infoBeen;
            notifyDataSetChanged();
        }
    }

    public void clear() {
        infoBeen.clear();
    }

    public interface deleteOnclickListener {
        void onClick(int position, View view);
    }
}
